package cn.com.xinjinfu.pdoulcProduct.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMVideo;
import com.umeng.socialize.utils.Log;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.login.CheckMobileActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.user.MyFriendsListAct;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ActivityManager;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;

/**
 * 邀请好友
 * Created by android-adt on 2016/4/14.
 */
public class InviteFriendsAct extends BaseActivity implements View.OnClickListener{
    UMImage image = new UMImage(this, R.drawable.icon_pdou180);
    String url = DataHandler.WEBURL+"/front/wa/appShare";
    String imgUrl = DataHandler.WEBURL+"/public/images/client_download.png";

    private ImageLoader imageLoader;
    private DisplayImageOptions options;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_invite_friends);
        ActivityManager.getInstance().addActivity(this);
        TitleManager.showTitle(this, "邀请好友", "已邀请");
        TextView invite_code = (TextView) findViewById(R.id.invite_code);
        LinearLayout top_right_ll = (LinearLayout) findViewById(R.id.top_right_ll);
        top_right_ll.setOnClickListener(this);
        User user = AccountManager.getInstance().getUser();
        if (user != null && user.getCurrUser().isLogged()) {
            invite_code.setText(user.getShareCode());
            url = url + "?code="+user.getShareCode();
            invite_code.setTextColor(0xffF77F96);
            invite_code.setTextSize(26);
            invite_code.setEnabled(false);
        }else {
            invite_code.setText("登录查看");
            invite_code.setTextColor(0xffCCCCCC);
            invite_code.setTextSize(17);
            invite_code.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(InviteFriendsAct.this, CheckMobileActivity.class));
                }
            });
        }
        ImageView img_invite = (ImageView) findViewById(R.id.img_invite);
//        ImageView xinlang = (ImageView) findViewById(R.id.xinlang);
        ImageView weixin_frient = (ImageView) findViewById(R.id.weixin_frient);
        ImageView weixin_fenxiang = (ImageView) findViewById(R.id.weixin_fenxiang);
//        xinlang.setOnClickListener(this);
        weixin_frient.setOnClickListener(this);
        weixin_fenxiang.setOnClickListener(this);
        if (!DataHandler.isNetworkConnected(this)) {
            ToastManager.show(this, "请检查网络连接");
            return;
        }


        // 初始化imageLoader 否则会报错
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(this));
        options = new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.client_download) // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.client_download) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.client_download) // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true) // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
                .build();
        imageLoader.displayImage( imgUrl,img_invite, options);

    }

    @Override
    public void onClick(View v) {
        if (!DataHandler.isNetworkConnected(this)) {
            ToastManager.show(this, "请检查网络连接");
            return;
        }else {
            switch (v.getId()) {
//                case R.id.xinlang:
//                    new ShareAction(this).setPlatform(SHARE_MEDIA.SINA).setCallback(umShareListener)
//                            .withText("我在P兜理财领取了3500元红包，下载并注册，还有更多红包奖励！")
//                            .withMedia(image)
////                     .withExtra(new UMImage(ShareActivity.this,R.drawable.ic_launcher))
//                            .withTargetUrl(url)
//                            .share();
//                    break;
                case R.id.weixin_frient:
                    new ShareAction(this).setPlatform(SHARE_MEDIA.WEIXIN).setCallback(umShareListener)
                            .withTitle(getString(R.string.weixin_titl))
                            .withMedia(image)
//                      .withMedia(new UMEmoji(ShareActivity.this,"http://img.newyx.net/news_img/201306/20/1371714170_1812223777.gif"))
                            .withText(getString(R.string.weixin_text))
                            .withTargetUrl(url)
                            .share();

                    break;
                case R.id.weixin_fenxiang:
                    new ShareAction(this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(umShareListener)
                            .withMedia(image)
                            .withTitle(getString(R.string.pengyouquan_titl))
                            .withText(getString(R.string.pengyouquan_text))
                            .withTargetUrl(url)
                            .share();
                    break;
                case R.id.top_right_ll:
                    User user = AccountManager.getInstance().getUser();
                    if (user != null && user.getCurrUser().isLogged()) {
                        startActivity(new Intent(this, MyFriendsListAct.class));
                    } else {
                        startActivity(new Intent(this, CheckMobileActivity.class));
                    }
                    break;
            }
        }
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Log.d("plat", "platform" + platform);
            Toast.makeText(InviteFriendsAct.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(InviteFriendsAct.this,platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(InviteFriendsAct.this,platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };
}
